﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Service;
using Infrastructure.Service;
using OMS.Models;

namespace OMS.Controllers
{
    public class MerchandiseController : Controller
    {
       
        public IMerchandiseService _service = new MerchandiseService();
        
        //
        // GET: /Merchandise/

        public ActionResult Index()
        {
            //ViewBag.merch = User.Identity.Name;
            ViewBag.currentPage = 1;
            ViewBag.itemOnPage = 10;

            ViewBag.Orders = _service.GetPaginationListOfOrders(1, ViewBag.itemOnPage, null);
            ViewBag.OrdersCount = _service.GetCountOfOrders();

            if (ViewBag.OrdersCount % ViewBag.itemOnPage != 0) ViewBag.lastPage = ViewBag.OrdersCount / ViewBag.itemOnPage + 1;
            else ViewBag.lastPage = ViewBag.OrdersCount / ViewBag.itemOnPage;

           
            return View();
        }






        public ActionResult OrderTable(int? page, int? itemOnPage, string orderOrdersBy, string searchString, string listSelectField, string listSelectCondition)
        {
            //ViewBag.SortById = String.IsNullOrEmpty(orderUsersBy) ? "UserName desc" : "";
            ViewBag.SortById = orderOrdersBy == "OrderId" ? "OrderId desc" : "OrderId";
            ViewBag.SortByCustomer = orderOrdersBy == "UserId" ? "UserId desc" : "UserId";
            ViewBag.SortByMerchandiser = orderOrdersBy == "Assignee" ? "Assignee desc" : "Assignee";
            ViewBag.SortByDate = orderOrdersBy == "DeliveryDate" ? "DeliveryDate desc" : "DeliveryDate";
            ViewBag.SortByPrice = orderOrdersBy == "Accepted" ? "Accepted desc" : "Accepted";
            ViewBag.SortByPrice = orderOrdersBy == "Delivered" ? "Delivered desc" : "Delivered";

            //ViewBag.SortByFirstName = orderUsersBy == "FirstName" ? "FirstName desc" : "FirstName";
           // ViewBag.SortByLastName = orderUsersBy == "LastName" ? "LastName desc" : "LastName";
           // ViewBag.SortByRole = orderUsersBy == "Role" ? "Role desc" : "Role";
            //ViewBag.SortByEmail = orderUsersBy == "Email" ? "Email desc" : "Email";
            //ViewBag.SortByRegion = orderUsersBy == "Region" ? "Region desc" : "Region";


            int currentPage = page ?? 1;
            ViewBag.itemOnPage = itemOnPage ?? 10;

            if (String.IsNullOrWhiteSpace(searchString))
            {
                ViewBag.Orders = _service.GetPaginationListOfOrders(currentPage, ViewBag.itemOnPage, orderOrdersBy);
                ViewBag.OrdersCount = _service.GetCountOfOrders();
            }
            else
            {
                ViewBag.Orders = _service.GetPaginationWithFilter(currentPage, ViewBag.itemOnPage, orderOrdersBy, searchString, listSelectField, listSelectCondition);
                ViewBag.OrdersCount = _service.GetCountOfOrders(searchString, listSelectField, listSelectCondition);
            }

            if (ViewBag.OrdersCount % ViewBag.itemOnPage != 0) ViewBag.lastPage = ViewBag.OrdersCount / ViewBag.itemOnPage + 1;
            else ViewBag.lastPage = ViewBag.OrdersCount / ViewBag.itemOnPage;

            ViewBag.currentPage = currentPage;
            ViewBag.currentSortOrdersBy = orderOrdersBy;
            ViewBag.searchString = searchString;
            ViewBag.listSelectField = listSelectField;
            ViewBag.listSelectCondition = listSelectCondition;

            //prov.UpdateUserLastActivityDate(User.Identity.Name);
           // ViewBag.numberOfUsersOnline = prov.GetNumberOfUsersOnline();
           // ViewBag.dictionaryOfUsersOnline = prov.GetDictionaryOfUsersOnline();

            return PartialView();
        }


        [HttpGet]
        public ActionResult Edit(int orderId, string userLogin)
        {

            //TODO: Check userLogin for changing only own assigned orders

            var order = _service.GetOrder(orderId);
            var model = new EditOrderModel();

            model.OrderId = orderId;
            model.Accepted = order.Accepted;
            model.Delivered = order.Delivered;
            model.Gift = order.Gift;
            model.DeliveryDate = order.DeliveryDate.ToString();

            var orderDetailsOfCurrentOrder = _service.GetOrdersDetailsOfOrder(orderId);
            ViewBag.orderDetails = orderDetailsOfCurrentOrder;
            ViewBag.order = order;
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(EditOrderModel model)
        {
            var order = _service.GetOrder(model.OrderId);
            order.Accepted = model.Accepted;
            order.Gift = model.Gift;
            order.Delivered = model.Delivered;
            if (model.DeliveryDate != null)
            {
                try
                {
                    var usCulture = "en-US";
                    order.DeliveryDate = DateTime.Parse(model.DeliveryDate, new CultureInfo(usCulture, false));
                }

                catch (FormatException)
                {
                    throw new FormatException("Invalid Format of DateTime");
                }
            }

            else
            {
                order.DeliveryDate = null;
            }
            // order.DeliveryDate = model.DeliveryDate;

            bool check = _service.EditOrder(order);
            return RedirectToAction("Index");

        }

    //    [HttpPost]
    //    public ActionResult Edit(EditUser model)
       // {
            //if (ModelState.IsValid)
            //{
            //    var user = _service.GetUser(model.LoginName);
            //    user.FirstName = model.FirstName;
            //    user.LastName = model.LastName;
            //    user.Password = model.Password;
            //    user.Email = model.Email;
            //    user.Region = model.Region;
            //    user.RoleId = model.Role;

            //    bool check = _service.EditUser(user);
            //    return RedirectToAction("Index");
            //}
            //else
            //{
            //    ModelState.AddModelError("", "User not valid");
            //}
            //return View(model);
      //  }


    }
}
